import java.util.concurrent.TimeUnit;

/**
 * Create by chenhd on 2021/8/28.
 * 方法二十：线程挂起法。这里只是作为一种演示，实际上suspend方法已经被弃用，因为suspend不会释放当前锁，如果resume的线程需要获取锁才能执行，就会造成死锁
 */
public class ConcurrentTask20 {
    public String str;

    public static void main(String[] args) {
        ConcurrentTask20 concurrentTask = new ConcurrentTask20();
        Thread currentThread = Thread.currentThread();
        new Thread(() -> concurrentTask.putStr(currentThread)).start();
        currentThread.suspend();
        System.out.println(concurrentTask.getStr());
    }

    public void putStr(Thread thread) {
        try {
            TimeUnit.SECONDS.sleep(5);
            this.str = "hello concurrent.";
            thread.resume();
        } catch (InterruptedException e) {
            //log error
            e.printStackTrace();
        }
    }

    public String getStr() {
        return str;
    }
}
